INC := ../include -I../arch/rlx/include
LDS := init.lds
OBJS := init.o ../board/rlxboard/bsp_init.o ../board/rlxboard/spic_ctrl.o

ifdef BOOTLOADER_OFFSET
ifeq ($(BOOTLOADER_OFFSET),0x50000)
	FLASHBASE ?= 0xbfc50000
else
	FLASHBASE ?= 0xbfc30000
endif
else
	FLASHBASE ?= 0xbfc30000
endif

ifeq ($(DECOMPRESS),1)
	RAMBASE ?= 0x80100000
else
	RAMBASE ?= 0x82800000
endif
PTR_COUNT_SHIFT ?=2

CROSS_COMPILE ?= mips-linux-

CC := $(CROSS_COMPILE)gcc
LD := $(CROSS_COMPILE)ld
OBJCPY := $(CROSS_COMPILE)objcopy


GCCINC := $(shell $(CC) -print-file-name=include)
GCCLIB := $(shell dirname `$(CC) -print-libgcc-file-name`)

GENLDSFLAGS := -I$(INC) -DCONFIG_SYS_RAM_BASE=$(RAMBASE) -DPTR_COUNT_SHIFT=$(PTR_COUNT_SHIFT) -ansi -E -P -

CFLAGS := -Os -ffunction-sections -fdata-sections -D__KERNEL__ \
	-DCONFIG_RAM_OFFSET=$(FLASH_VERSION_SIZE) \
	-DCONFIG_RAM_SIZE=$(RAM_VERSION_SIZE) \
	-DCONFIG_SYS_RAM_BASE=$(RAMBASE) -I$(INC) -fno-builtin \
	-ffreestanding -nostdinc -isystem $(GCCINC) -pipe -G 0 -mabicalls \
	-fPIC -EL -msoft-float -fno-strict-aliasing -mabi=32 -Wall \
	-Wstrict-prototypes -fno-stack-protector -Wno-format-nonliteral \
	-Wno-format-security -fstack-usage

ASFLAGS := $(CFLAGS) -D__ASSEMBLY__

LDFLAGS := -G 0 -static -n -nostdlib -EL -m elf32ltsmip -T $(LDS) \
	--gc-sections -pie -Bstatic -Ttext $(FLASHBASE) -L $(GCCLIB) -lgcc

OBJFLAGS := --remove-section=.dynsym --gap-fill=0xff -O binary

.PHONY : all .depend
all : $(LDS) .depend init.bin
#$(LDS) : include/$(LDS)
#	$(CC) $(GENLDSFLAGS) < include/$(LDS) > $(LDS)
#.depend :
#	$(CC) $(CFLAGS) -MM $(SRC) > .depend
#-include .depend

init : $(OBJS) $(LDS)
	$(LD) $(LDFLAGS) $(OBJS) -Map init.map -o init
init.bin : init
	$(OBJCPY) $(OBJFLAGS) init init.bin

.PHONY : clean
clean :
	rm -f *.o *.su .depend init.map init *.bin
	rm -f $(OBJS)
